package de.melistik.smugmugsync.connect;

import java.util.List;
import java.util.logging.Logger;

import com.kallasoft.smugmug.api.json.entity.Album;
import com.kallasoft.smugmug.api.json.entity.Category;
import com.kallasoft.smugmug.api.json.v1_2_1.APIVersionConstants;

public class SmugMugBaseRW {

	Logger LOGGER = Logger.getLogger(SmugMugBaseRW.class.getName());
	private SmugMugConnection connection;
	String setPassword = null;
	boolean isPublic = false;

	public SmugMugBaseRW(SmugMugConnection connection) {
		this.connection = connection;
	}

	public List<Album> getAlbumList() {
		com.kallasoft.smugmug.api.json.v1_2_1.albums.Get albumGet = new com.kallasoft.smugmug.api.json.v1_2_1.albums.Get();
		com.kallasoft.smugmug.api.json.v1_2_1.albums.Get.GetResponse albums = albumGet
				.execute(APIVersionConstants.SECURE_SERVER_URL,
						SmugMugConnection.API_KEY, connection.getResponse()
								.getSessionID(), false);
		return albums.getAlbumList();
	}

	public Album getAlbumById(int albumId) {
		Album album = null;
		for (Album a : getAlbumList()) {
			if (a.getID() == albumId) {
				album = a;
			}
		}
		return album;
	}

	public Album findAlbumInList(List<Album> albumList, String albumTitle,
			int categoryID) {
		Album album = null;
		for (Album a : getAlbumList()) {
			if (a.getTitle().equalsIgnoreCase(albumTitle.toLowerCase())
					&& a.getCategory().getID() == categoryID) {
				album = a;
			}
		}
		return album;
	}

	public int createAlbum(String title, int categoryID) {
		com.kallasoft.smugmug.api.json.v1_2_1.albums.Create albumCreate = new com.kallasoft.smugmug.api.json.v1_2_1.albums.Create();
		com.kallasoft.smugmug.api.json.v1_2_1.albums.Create.CreateResponse response = albumCreate
				.execute(APIVersionConstants.SECURE_SERVER_URL,
						SmugMugConnection.API_KEY, connection.getResponse()
								.getSessionID(), title, null, null, categoryID,
						null, null, null, null, null, null, null, null, null,
						null, null, null, setPassword, null, null, isPublic,
						null, null, null, null, null, null, null, null, null,
						null, null, null, null, null, null, null, null, null,
						null, null, null, null, null, null, null);
		return response.getAlbumID();
	}

	public int findOrCreateAlbum(String title, int categoryID) {
		Album album = findAlbumInList(getAlbumList(), title, categoryID);
		if (album != null) {
			return album.getID();
		} else {
			return createAlbum(title, categoryID);
		}
	}

	public List<Category> getCategoryList() {
		com.kallasoft.smugmug.api.json.v1_2_1.categories.Get categoryGet = new com.kallasoft.smugmug.api.json.v1_2_1.categories.Get();
		com.kallasoft.smugmug.api.json.v1_2_1.categories.Get.GetResponse categories = categoryGet
				.execute(APIVersionConstants.SECURE_SERVER_URL,
						SmugMugConnection.API_KEY, connection.getResponse()
								.getSessionID());
		return categories.getCategoryList();
	}

	public Category getCategoryById(int categoryID) {
		Category category = null;
		for (Category c : getCategoryList()) {
			if (c.getID() == categoryID) {
				category = c;
			}
		}
		return category;
	}

	public Category getCategoryByName(String categoryName) {
		Category category = null;
		for (Category c : getCategoryList()) {
			if (c.getName().equalsIgnoreCase(categoryName.toLowerCase())) {
				category = c;
			}
		}
		return category;
	}

	public int createCategory(String categoryName) {
		com.kallasoft.smugmug.api.json.v1_2_1.categories.Create categoryCreate = new com.kallasoft.smugmug.api.json.v1_2_1.categories.Create();
		com.kallasoft.smugmug.api.json.v1_2_1.categories.Create.CreateResponse response = categoryCreate
				.execute(APIVersionConstants.SECURE_SERVER_URL,
						SmugMugConnection.API_KEY, connection.getResponse()
								.getSessionID(), categoryName);
		return response.getCategoryID();
	}

	public int findOrCreateCategory(String categoryName) {
		Category category = getCategoryByName(categoryName);
		if (category != null) {
			return category.getID();
		} else {
			return createCategory(categoryName);
		}

	}

}
